package com.richapp.launcher.database.infrared.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;

import com.richapp.launcher.database.infrared.InfraredDatabaseHelper;
import com.richapp.launcher.database.infrared.mode.BrandModel;

import java.util.List;

public class BrandModelDao {

    private Context context;
    private InfraredDatabaseHelper helper = null;
    private Dao<BrandModel, Integer> dao = null;

    public BrandModelDao(Context context) {
        this.context = context;
        try {
            helper = InfraredDatabaseHelper.getHelper(this.context);
            dao = helper.getDao(BrandModel.class);
            dao.setObjectCache(false);
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public void add(BrandModel brandModel) {
        try {
            dao.createOrUpdate(brandModel);
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public void deleteAll() {
        try {
            DeleteBuilder deleteBuilder = dao.deleteBuilder();
            deleteBuilder.delete();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public List<BrandModel> getList(int machineTypeId, int brandId) {
        List<BrandModel> list = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("machineTypeId", machineTypeId).and().eq("brandId", brandId);
            list = queryBuilder.query();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return list;
    }

    public List<BrandModel> getListLike(int machineTypeId, int brandId, String modelName) {
        List<BrandModel> list = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("machineTypeId", machineTypeId).and().eq("brandId", brandId).and().like("modelName", "%" + modelName + "%");
            list = queryBuilder.query();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return list;
    }

    public BrandModel get(int machineTypeId, int brandId, String modelName) {
        BrandModel brandModel = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("machineTypeId", machineTypeId).and().eq("brandId", brandId).and().eq("modelName", modelName);
            Object obj = queryBuilder.queryForFirst();
            if (obj != null) {
                if (obj instanceof BrandModel) {
                    brandModel = (BrandModel) obj;
                }
            }
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return brandModel;
    }

    public BrandModel get(int brandModelId) {
        BrandModel brandModel = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("brandModelId", brandModelId);
            Object obj = queryBuilder.queryForFirst();
            if (obj != null) {
                if (obj instanceof BrandModel) {
                    brandModel = (BrandModel) obj;
                }
            }
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return brandModel;
    }
}
